package com.jpa.sgcrm;

import java.util.Locale;
import java.util.ResourceBundle;

import com.jpa.model.*;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItem;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class Mainview extends HorizontalSplitPanel implements
		ComponentContainer {

	private Table incomeTable;
	private Table outcomeTable;
	private Table resultTable;
	private Table resultTablewo;
	private Table incomeDetail;
	private Table outcomeDetail;
	private Button newButton;
	private Button deleteButton;
	private Button editButton;
	private Button outcomenewButton;
	private Button outcomedeleteButton;
	private Button outcomeeditButton;
	private Button reporting;
	private Button companyButton;
	private JPAContainer<Income> incomes;
	private JPAContainer<Outcome> outcomes;
	private Report report;
	public JPAContainer<TypeIn> con;
	public JPAContainer<TypeOut> con1;
	public JPAContainer<Company> company;
	public Locale locale;
	public ResourceBundle Messages;

	public Mainview() {
		Messages = ResourceBundle.getBundle("messages");
		incomes = JPAContainerFactory.make(Income.class,
				SgcrmUI.PERSISTENCE_UNIT);
		outcomes = JPAContainerFactory.make(Outcome.class,
				SgcrmUI.PERSISTENCE_UNIT);
		con = JPAContainerFactory.make(TypeIn.class, SgcrmUI.PERSISTENCE_UNIT);
		con1 = JPAContainerFactory
				.make(TypeOut.class, SgcrmUI.PERSISTENCE_UNIT);
		company = JPAContainerFactory
				.make(Company.class, SgcrmUI.PERSISTENCE_UNIT);
		buildResultTable();
		buildDetailTable();
		buildMainIncome();
		buildMainOutcome();

	}

	SgcrmUI uiParent;

	public SgcrmUI getUiParent() {
		return uiParent;
	}

	public void setUiParent(SgcrmUI uiParent) {
		this.uiParent = uiParent;
	}

	public Mainview(Locale locale) {
		this.locale = locale;
		Messages = ResourceBundle.getBundle("messages", locale);
		incomes = JPAContainerFactory.make(Income.class,
				SgcrmUI.PERSISTENCE_UNIT);
		outcomes = JPAContainerFactory.make(Outcome.class,
				SgcrmUI.PERSISTENCE_UNIT);
		con = JPAContainerFactory.make(TypeIn.class, SgcrmUI.PERSISTENCE_UNIT);
		con1 = JPAContainerFactory
				.make(TypeOut.class, SgcrmUI.PERSISTENCE_UNIT);
		company = JPAContainerFactory
				.make(Company.class, SgcrmUI.PERSISTENCE_UNIT);
		buildResultTable();
		buildDetailTable();
		buildMainIncome();
		buildMainOutcome();

	}

	private void buildDetailTable() {
		incomeDetail = new Table();
		incomeDetail.setImmediate(true);
		incomeDetail.addContainerProperty(
				Messages.getString("Mainview.type"), String.class, null); //$NON-NLS-1$
		incomeDetail.addContainerProperty(
				Messages.getString("Mainview.in"), Integer.class, null); //$NON-NLS-1$
		incomeDetail.addContainerProperty(
				Messages.getString("Mainview.inwovat"), Integer.class, null); //$NON-NLS-1$
		outcomeDetail = new Table();
		outcomeDetail.setImmediate(true);
		outcomeDetail.addContainerProperty(
				Messages.getString("Mainview.type"), String.class, null); //$NON-NLS-1$
		outcomeDetail.addContainerProperty(
				Messages.getString("Mainview.out"), Integer.class, null); //$NON-NLS-1$
		outcomeDetail.addContainerProperty(
				Messages.getString("Mainview.outwovat"), Integer.class, null); //$NON-NLS-1$

	}

	private void buildResultTable() {
		resultTable = new Table(Messages.getString("Mainview.montlyreport")); //$NON-NLS-1$
		resultTablewo = new Table(
				Messages.getString("Mainview.montlyreportwovat")); //$NON-NLS-1$
		resultTable.setImmediate(true);
		resultTable.setSelectable(true);
		resultTable
				.addItemClickListener(new ItemClickEvent.ItemClickListener() {

					@Override
					public void itemClick(ItemClickEvent event) {
						// TODO Auto-generated method stub
						if (event.isDoubleClick()) {
							if (event.getPropertyId().toString()
									.equals(Messages.getString("Mainview.in"))) { //$NON-NLS-1$
								int month = (Integer) event
										.getItem()
										.getItemProperty(
												Messages.getString("Mainview.month")).getValue(); //$NON-NLS-1$
								Detail detail = new Detail(incomeTable, month,
										true, Messages);
								detail.showDetail(incomeDetail);
								Window w = new Window();
								VerticalLayout v = new VerticalLayout();
								w.setContent(v);
								StringBuilder sb = new StringBuilder();
								sb.append(month);
								sb.append(Messages.getString("Mainview.DRR")); //$NON-NLS-1$
								w.setCaption(sb.toString());
								v.addComponent(incomeDetail);
								UI.getCurrent().addWindow(w);
								w.center();
							} else if (event.getPropertyId().toString()
									.equals(Messages.getString("Mainview.out"))) { //$NON-NLS-1$
								int month = (Integer) event
										.getItem()
										.getItemProperty(
												Messages.getString("Mainview.month")).getValue(); //$NON-NLS-1$
								Detail detail = new Detail(outcomeTable, month,
										false, Messages);
								detail.showDetail(outcomeDetail);
								Window w = new Window();
								VerticalLayout v = new VerticalLayout();
								w.setContent(v);
								StringBuilder sb = new StringBuilder();
								sb.append(month);
								sb.append(Messages.getString("Mainview.DSR")); //$NON-NLS-1$
								w.setCaption(sb.toString());
								v.addComponent(outcomeDetail);
								UI.getCurrent().addWindow(w);
								w.center();
							}
						}
					}
				});

		resultTable.addContainerProperty(
				Messages.getString("Mainview.month"), Integer.class, null); //$NON-NLS-1$
		resultTable.addContainerProperty(
				Messages.getString("Mainview.in"), Integer.class, null); //$NON-NLS-1$
		resultTable.addContainerProperty(
				Messages.getString("Mainview.out"), Integer.class, null); //$NON-NLS-1$
		resultTable.addContainerProperty(
				Messages.getString("Mainview.balance"), Integer.class, null); //$NON-NLS-1$
		resultTablewo.setImmediate(true);
		resultTablewo.setSelectable(true);
		resultTablewo.addContainerProperty(
				Messages.getString("Mainview.month"), Integer.class, null); //$NON-NLS-1$
		resultTablewo.addContainerProperty(
				Messages.getString("Mainview.in"), Integer.class, null); //$NON-NLS-1$
		resultTablewo.addContainerProperty(
				Messages.getString("Mainview.out"), Integer.class, null); //$NON-NLS-1$
		resultTablewo.addContainerProperty(
				Messages.getString("Mainview.balance"), Integer.class, null); //$NON-NLS-1$

	}

	private void buildMainIncome() {

		VerticalLayout vl = new VerticalLayout();
		setFirstComponent(vl);
		incomeTable = new Table(null, incomes);
		incomeTable.setSelectable(true);
		incomeTable.setImmediate(true);
		incomeTable.setCaption(Messages.getString("Mainview.revenue")); //$NON-NLS-1$
		Object[] properties = { "month", "date" }; //$NON-NLS-1$ //$NON-NLS-2$
		boolean[] ordering = { true, false };
		incomeTable.sort(properties, ordering);
		incomeTable.addValueChangeListener(new Property.ValueChangeListener() {

			@Override
			public void valueChange(ValueChangeEvent event) {
				setModificationsEnabled(event.getProperty().getValue() != null);
			}

			private void setModificationsEnabled(boolean b) {
				deleteButton.setEnabled(b);
				editButton.setEnabled(b);
			}
		});
		incomeTable.setSizeFull();

		incomeTable.addItemClickListener(new ItemClickListener() {

			@Override
			public void itemClick(ItemClickEvent event) {
				if (event.isDoubleClick()) {

					incomeTable.select(event.getItemId());
				}
			}

		});
		incomeTable.setVisibleColumns(new Object[] { "month", "type", "name", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				"company", "date", "paid", "price", "vat", "total" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
		incomeTable.setColumnWidth("date", 69); //$NON-NLS-1$
		incomeTable.setColumnWidth("paid", 69); //$NON-NLS-1$

		HorizontalLayout tb = new HorizontalLayout();
		newButton = new Button(Messages.getString("Mainview.add")); //$NON-NLS-1$
		newButton.addClickListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				final BeanItem<Income> newIncome = new BeanItem<Income>(
						new Income());
				Editor personEditor = new Editor(newIncome, true, con, con1,
						company,Messages);
				personEditor
						.addListener(new com.jpa.sgcrm.Editor.EditorSavedListener() {
							@Override
							public void editorSaved(
									com.jpa.sgcrm.Editor.EditorSavedEvent event) {
								incomes.addEntity(newIncome.getBean());
								Object[] properties = { "month", "date" }; //$NON-NLS-1$ //$NON-NLS-2$
								boolean[] ordering = { true, true };
								incomeTable.sort(properties, ordering);

							}
						});
				UI.getCurrent().addWindow(personEditor);
				personEditor.setHeight("50%"); //$NON-NLS-1$
				personEditor.setWidth("20%"); //$NON-NLS-1$
				personEditor.center();
			}

		});
		deleteButton = new Button(Messages.getString("Mainview.delete")); //$NON-NLS-1$
		deleteButton.addClickListener(new ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				incomeTable.removeItem(incomeTable.getValue());
			}

		});
		deleteButton.setEnabled(false);

		editButton = new Button(Messages.getString("Mainview.edit")); //$NON-NLS-1$
		editButton.addClickListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				UI.getCurrent().addWindow(
						new Editor(incomeTable.getItem(incomeTable.getValue()),
								true, con, con1,company,Messages));

			}
		});
		editButton.setEnabled(false);

		reporting = new Button(Messages.getString("Mainview.report")); //$NON-NLS-1$
		reporting.addClickListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				Window window = new Window();
				VerticalLayout v = new VerticalLayout();
				window.setContent(v);
				window.setCaption(Messages.getString("Mainview.report")); //$NON-NLS-1$
				report = new Report(incomes, outcomes, Messages);
				report.showTotal(resultTable);
				report.showTotalwo(resultTablewo);
				v.addComponent(resultTable);
				v.addComponent(resultTablewo);
				UI.getCurrent().addWindow(window);
				window.center();
			}
		});
		companyButton = new Button("Company");
		companyButton.addClickListener(new ClickListener(){

			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				final BeanItem<Company> companyItem = new BeanItem<Company>(
						new Company());
				Editor personEditor = new Editor(companyItem, true, con, con1,
						company,Messages);
				personEditor
						.addListener(new com.jpa.sgcrm.Editor.EditorSavedListener() {
							@Override
							public void editorSaved(
									com.jpa.sgcrm.Editor.EditorSavedEvent event) {
								company.addEntity(companyItem.getBean());
							//	Object[] properties = { "month", "date" }; //$NON-NLS-1$ //$NON-NLS-2$
							//	boolean[] ordering = { true, true };
								//incomeTable.sort(properties, ordering);

							}
						});
				UI.getCurrent().addWindow(personEditor);
				personEditor.setHeight("50%"); //$NON-NLS-1$
				personEditor.setWidth("20%"); //$NON-NLS-1$
				personEditor.center();
			}
			
		});

		tb.addComponent(newButton);
		tb.addComponent(deleteButton);
		tb.addComponent(editButton);
		tb.addComponent(reporting);
		tb.addComponent(companyButton);
		tb.setComponentAlignment(reporting, Alignment.TOP_RIGHT);

		tb.setWidth("100%"); //$NON-NLS-1$
		vl.addComponent(tb);
		vl.addComponent(incomeTable);
		vl.setExpandRatio(incomeTable, 1);
		tb.setExpandRatio(reporting, 1);
		vl.setSizeFull();

	}

	private void buildMainOutcome() {
		VerticalLayout vl = new VerticalLayout();
		setSecondComponent(vl);

		outcomeTable = new Table(null, outcomes);
		outcomeTable.setSelectable(true);
		outcomeTable.setImmediate(true);
		outcomeTable.setCaption(Messages.getString("Mainview.purchase")); //$NON-NLS-1$
		Object[] properties = { "month", "date" }; //$NON-NLS-1$ //$NON-NLS-2$
		boolean[] ordering = { true, true };
		outcomeTable.sort(properties, ordering);
		outcomeTable.addValueChangeListener(new Property.ValueChangeListener() {

			@Override
			public void valueChange(ValueChangeEvent event) {
				setModificationsEnabled(event.getProperty().getValue() != null);
			}

			private void setModificationsEnabled(boolean b) {
				outcomedeleteButton.setEnabled(b);
				outcomeeditButton.setEnabled(b);
			}
		});
		outcomeTable.setSizeFull();

		outcomeTable.addItemClickListener(new ItemClickListener() {

			@Override
			public void itemClick(ItemClickEvent event) {
				if (event.isDoubleClick()) {

					outcomeTable.select(event.getItemId());
				}
			}

		});
		outcomeTable.setVisibleColumns(new Object[] { "month", "type", "name", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				"company", "date", "paid", "price", "vat", "total" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
		outcomeTable.setColumnWidth("date", 69); //$NON-NLS-1$
		outcomeTable.setColumnWidth("paid", 69); //$NON-NLS-1$

		HorizontalLayout tb = new HorizontalLayout();
		outcomenewButton = new Button(Messages.getString("Mainview.add")); //$NON-NLS-1$
		outcomenewButton.addClickListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				final BeanItem<Outcome> newIncome = new BeanItem<Outcome>(
						new Outcome());
				Editor personEditor = new Editor(newIncome, false, con, con1,
						company,Messages);
				personEditor
						.addListener(new com.jpa.sgcrm.Editor.EditorSavedListener() {
							@Override
							public void editorSaved(
									com.jpa.sgcrm.Editor.EditorSavedEvent event) {
								outcomes.addEntity(newIncome.getBean());
								Object[] properties = { "month", "date" }; //$NON-NLS-1$ //$NON-NLS-2$
								boolean[] ordering = { true, false };
								outcomeTable.sort(properties, ordering);
							}
						});
				UI.getCurrent().addWindow(personEditor);
				personEditor.setHeight("50%"); //$NON-NLS-1$
				personEditor.setWidth("20%"); //$NON-NLS-1$
				personEditor.center();
			}

		});
		outcomedeleteButton = new Button(Messages.getString("Mainview.delete")); //$NON-NLS-1$
		outcomedeleteButton.addClickListener(new ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				outcomeTable.removeItem(outcomeTable.getValue());
			}

		});
		outcomedeleteButton.setEnabled(false);

		outcomeeditButton = new Button(Messages.getString("Mainview.edit")); //$NON-NLS-1$
		outcomeeditButton.addClickListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				UI.getCurrent().addWindow(
						new Editor(
								outcomeTable.getItem(outcomeTable.getValue()),
								false, con, con1, company,Messages));

			}
		});
		outcomeeditButton.setEnabled(false);

		tb.addComponent(outcomenewButton);
		tb.addComponent(outcomedeleteButton);
		tb.addComponent(outcomeeditButton);
		NativeSelect ns = new NativeSelect();
		ns = buildLanguage(ns);
		tb.addComponent(ns);
		tb.setComponentAlignment(ns, Alignment.TOP_RIGHT);

		tb.setWidth("100%"); //$NON-NLS-1$
		vl.addComponent(tb);
		vl.addComponent(outcomeTable);
		vl.setExpandRatio(outcomeTable, 1);
		tb.setExpandRatio(ns, 1);
		vl.setSizeFull();

	}

	private NativeSelect buildLanguage(NativeSelect ns) {

		ns.setImmediate(true);
		ns.addItem("English");
		ns.addItem("Korean");
		ns.setCaption("Language");
		ns.addValueChangeListener(new Property.ValueChangeListener() {

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if (event.getProperty().getValue().equals("Korean")) {
					Locale locale = new Locale("ko", "KR");

					getUiParent().setContent(new Mainview(locale));

				} else if (event.getProperty().getValue().equals("English")) {
					getUiParent().setContent(new Mainview());
				}
			}
		});
		return ns;

	}
}
